#include "rankdb.h"

RankDB::RankDB(QObject *parent) :
    QObject(parent)
{
}

/**
 * @brief 将成绩存入数据库
 *
 * @param score 分数
 */
void RankDB::saveScore(const QString &score)
{
    UsersDB *userdb = new UsersDB;
    UserModel user = userdb->getUserInfo();

    QString tablename;

    switch (user.difficulty)
    {
    case 0:
        tablename = "simpleRank";
        break;
    case 1:
        tablename = "generalRank";
        break;
    case 2:
        tablename = "difficultRank";
        break;
    default:
        break;
    }

    // 使用QSqlQuery查询连接整张表，先要使用连接名获取该连接
    QSqlDatabase db = QSqlDatabase::database("my.db");
    QSqlQuery query(db);
    query.exec(QString("insert into %1 values( NULL, '%2', '%3')")
               .arg(tablename).arg(user.name).arg(score));
    if ( score.toInt() > user.maxscore)
        userdb->updateMaxScore(score);

    return;
}
